<?php

class Pageley_Mock_Factory {
    private $proxyFactory;
    private $invocationHandlers;
    private $counter = 0;
    
    public function __construct() {
        $this->proxyFactory = new Pageley_Reflection_ProxyFactory();
        $this->invocationHandlers = new Pageley_Util_ArrayMap();
    }

    public function createMock($interfaces) {
        $invocationHandler = new Pageley_Mock_RecordingInvocationHandler($this);
        $mock = $this->proxyFactory->createProxy($interfaces, $invocationHandler);
        $this->invocationHandlers->add($invocationHandler);
        return $mock;
    }
    
    public function replay() {
        foreach($this->invocationHandlers as $invocationHandler) {
            $invocationHandler->replay();
        }
        $this->counter = 0;
    }
    
    public function record() {
        foreach($this->invocationHandlers as $invocationHandler) {
            $invocationHandler->record();
        }
    }
    
    public function finish() {
        $unexecutedRecordings = new Pageley_Util_ArrayMap();
        foreach($this->invocationHandlers as $invocationHandler) {
            $recordings = $invocationHandler->getRecordings();
            if($recordings->size() > 0) {
                $unexecutedRecordings->addAll($recordings->keys());
            }
        }
        if($unexecutedRecordings->size() > 0) {
            throw new Pageley_Test_AssertionException("Expected more invocations: " . $unexecutedRecordings);
        }
    }
    
    public function count() {
        $this->counter++;
        return $this->counter;
    }
}